package com.example.administrator.nl.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.nl.R;
import com.example.administrator.nl.adapter.HomeAdapter;
import com.example.administrator.nl.base.BaseActivity;
import com.example.administrator.nl.bean.HomeBean;
import com.example.administrator.nl.util.ToastUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SearchActivity extends BaseActivity {
    @BindView(R.id.id_flowlayout)
    TagFlowLayout id_flowlayout;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @BindView(R.id.iv_finish_search)
    ImageView iv_finish;

    @BindView(R.id.tv_cancel_search)
    TextView tv_cancel_search;

    @BindView(R.id.et_content_search)
    EditText et_content_search;

    List<String> list;
    List<HomeBean> shoppings;

    private HomeAdapter homeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener();
    }


    @Override
    public int getContentView() {
        return R.layout.activity_search;
    }


    @Override
    public void init() {
        searchData();
        shoppings = new ArrayList<>();
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        shoppings.add(new HomeBean());
        recyclerView.setLayoutManager(new GridLayoutManager(SearchActivity.this, 2));
        homeAdapter = new HomeAdapter(R.layout.item_home_layout, shoppings);
        recyclerView.setAdapter(homeAdapter);

    }

    private void searchData() {
        // 搜索数据
        getSearchData();

        // 选中数量为1
        id_flowlayout.setMaxSelectCount(1);
        id_flowlayout.setAdapter(new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(SearchActivity.this).inflate(R.layout.label_search,
                        null);
                tv.setText(s);
                return tv;
            }
        });
    }

    private void listener() {
        // 搜索标签点击事件
        id_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                ToastUtil.ToastShort(list.get(position) + "");
                return true;
            }
        });

        iv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_cancel_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_content_search.setText("");
            }
        });
    }

    // 热门搜索数据
    public void getSearchData() {
        list = new ArrayList<>();
        list.add("是阿萨德");
        list.add("是阿");
        list.add("是阿萨德");
        list.add("是阿萨德");
        list.add("是阿萨德");
        list.add("是阿萨德是阿萨德是阿萨德");
        list.add("是阿");
        list.add("是阿萨德");
        list.add("123");
        list.add("asd");
        list.add("asadasdasdd");
    }
}
